import 'package:hrf_app/common/api/bug/bug.dart';
import 'package:hrf_app/common/api/material/material.dart';
import 'package:hrf_app/common/models/bug/project_bug_model.dart';
import 'package:hrf_app/common/models/material/material.dart';
import 'package:hrf_app/common/routers/names.dart';
import 'package:hrf_app/pages/menu/bug_page_list/projectBugPage/model/searchProjectBugModel.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:hrf_app/pages/menu/material/materialSearchListPage/model/searchMaterialModel.dart';

class MaterialSearchPageListController extends GetxController {
  MaterialSearchPageListController();

  List<MaterialModel> materialList = <MaterialModel>[];
  SearchMaterialModel searchMaterialModel = SearchMaterialModel();
  bool isLoading = false;
  late EasyRefreshController easyRefreshController;

  _initData() {
    getMaterialListPage(searchMaterialModel);
  }

  searchName(name) {
    searchMaterialModel.name = name;
    searchMaterialModel.pageNum = 1;
    getMaterialListPage(searchMaterialModel);
  }

  getMaterialListPage(params) {
    MaterialApi.getMaterialPageListApi(params).then((res) {
      if (res["code"] == "00000") {
        materialList = [];
        for (var item in res["data"]["list"]) {
          materialList.add(MaterialModel.fromJson(item));
        }
        update(["materialSearchPage"]);
      }
    });
  }

  onJumpToDetail(id) {
    Get.toNamed(RouteNames.materialDetailPage, arguments: id);
  }

  @override
  void onInit() {
    easyRefreshController = EasyRefreshController(
      controlFinishRefresh: true,
      controlFinishLoad: true,
    );

    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  void onRefresh() {
    searchMaterialModel.pageNum = 1;
    MaterialApi.getMaterialPageListApi(searchMaterialModel).then((res) {
      if (res["code"] == "00000") {
        materialList = [];
        for (var item in res["data"]["list"]) {
          materialList.add(MaterialModel.fromJson(item));
        }
        easyRefreshController.finishRefresh(IndicatorResult.success);
        easyRefreshController.resetFooter();
        update(["materialSearchPage"]);
      }
    });
  }

  void onLoad() {
    searchMaterialModel.pageNum = searchMaterialModel.pageNum + 1;
    MaterialApi.getMaterialPageListApi(searchMaterialModel).then((res) {
      if (res["code"] == "00000") {
        List<dynamic> projectBugResList = [];
        projectBugResList = res["data"]["list"];
        if (projectBugResList.isEmpty) {
          easyRefreshController.finishLoad(IndicatorResult.noMore);
          searchMaterialModel.pageNum--;
        }
        if (projectBugResList.isNotEmpty) {
          for (var item in projectBugResList) {
            materialList.add(MaterialModel.fromJson(item));
          }
          easyRefreshController.finishLoad();
        }
        update(["materialSearchPage"]);
      }
    });
  }

  void jumpToProjectButAdd() {
    Get.toNamed(RouteNames.materialAddPage);
  }

// @override
// void onClose() {
//   super.onClose();
// }
}
